package com.ruoyi.detect.service.Impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.detect.domain.Floor;
import com.ruoyi.detect.domain.Residential;
import com.ruoyi.detect.dto.Options;
import com.ruoyi.detect.mapper.FloorMapper;
import com.ruoyi.detect.mapper.ResidentialMapper;
import com.ruoyi.detect.service.IResidentialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 无Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-10
 */
@Service
public class ResidentialServiceImpl implements IResidentialService {
    @Autowired
    private ResidentialMapper residentialMapper;


    @Autowired
    private FloorMapper floorMapper;

    /**
     * 查询无
     *
     * @param id 无主键
     * @return 无
     */
    @Override
    public Residential selectResidentialById(Long id) {
        return residentialMapper.selectResidentialById(id);
    }

    /**
     * 查询无列表
     *
     * @param residential 无
     * @return 无
     */
    @Override
    public List<Residential> selectResidentialList(Residential residential) {
        return residentialMapper.selectResidentialList(residential);
    }

    /**
     * 新增无
     *
     * @param residential 无
     * @return 结果
     */
    @Transactional
    @Override
    public int insertResidential(Residential residential) {
        int rows = residentialMapper.insertResidential(residential);
        insertFloor(residential);
        return rows;
    }

    /**
     * 修改无
     *
     * @param residential 无
     * @return 结果
     */
    @Transactional
    @Override
    public int updateResidential(Residential residential) {
        residentialMapper.deleteFloorByResidentialId(residential.getId());
        insertFloor(residential);
        return residentialMapper.updateResidential(residential);
    }

    /**
     * 批量删除无
     *
     * @param ids 需要删除的无主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteResidentialByIds(Long[] ids) {
        residentialMapper.deleteFloorByResidentialIds(ids);
        return residentialMapper.deleteResidentialByIds(ids);
    }

    /**
     * 删除无信息
     *
     * @param id 无主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteResidentialById(Long id) {
        residentialMapper.deleteFloorByResidentialId(id);
        return residentialMapper.deleteResidentialById(id);
    }

    @Override
    public AjaxResult getList() {
        List<Residential> getlist = residentialMapper.getlist();
        List<Floor> list = floorMapper.getList();
        ArrayList<Options> optionsArrayList = new ArrayList<>();
        getlist.forEach(residential -> {
            List<Floor> collect = list.stream().filter(a -> Objects.equals(a.getResidential_id(), residential.getId())).collect(Collectors.toList());
            Options options = new Options();
            options.setId(residential.getId());
            options.setName(residential.getName());
            options.setChildren(collect);
            optionsArrayList.add(options);
        });
        return AjaxResult.success(optionsArrayList);
    }

    /**
     * 新增楼栋管理信息
     *
     * @param residential 无对象
     */
    public void insertFloor(Residential residential) {
        List<Floor> floorList = residential.getFloorList();
        Long id = residential.getId();
        if (StringUtils.isNotNull(floorList)) {
            List<Floor> list = new ArrayList<>();
            for (Floor floor : floorList) {
                floor.setResidential_id(id);
                list.add(floor);
            }
            if (list.size() > 0) {
                residentialMapper.batchFloor(list);
            }
        }
    }
}
